import axios from "axios";

const instance = axios.create({
    baseURL: 'http://127.0.0.1:9000',
    timeout: 5000
});

instance.interceptors.request.use(
    config => {
        console.log('---request---');
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);

instance.interceptors.response.use(
    response => {
        console.log('---response---');
        return response;
    },
    error => {
        return Promise.reject(error)
    }
);

export function get(url, params) {
    return instance.get(url, {
        params
    })
}

export function post(url, params) {
    return instance.post(url, params, {
        transformRequest: {
            function(data) {
                let str = '';
                for (let key in data) {
                    str += encodeURIComponent(key) + '='
                        + encodeURIComponent(data[key]) + '&';
                }
                return str;
            }
        },
        headers: {
            "Content-Type": "application/x-www-form-urlencoded"
        }
    })
}